/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "keyboardmain.h"

KeyboardMain::KeyboardMain() : mFirstLoad(true)
{
    pluginName = tr("Keyboard");
    pluginType = DEVICES;
}

KeyboardMain::~KeyboardMain()
{
}

QString KeyboardMain::plugini18nName()
{
    return pluginName;
}

int KeyboardMain::pluginTypes() {
    return pluginType;
}

QWidget *KeyboardMain::pluginUi()
{
    if (mFirstLoad) {
        mFirstLoad = false;
        keyBoardWidget = new KeyboardUi;
        keyBoardWidget->delayFrame()->slider()->installEventFilter(this);
        keyBoardWidget->speedFrame()->slider()->installEventFilter(this);
        keyBoardDbus = new QDBusInterface("org.ukui.ukcc.session",
                                           "/KeyBoard",
                                           "org.ukui.ukcc.session.KeyBoard",
                                           QDBusConnection::sessionBus(), this);

        if (!keyBoardDbus->isValid()) {
            qCritical() << "org.ukui.ukcc.session.KeyBoard DBus error:" << keyBoardDbus->lastError();
        } else {
            initKeyboardStatus();
            initConnection();
            QDBusConnection::sessionBus().connect("org.ukui.ukcc.session",
                                                  "/KeyBoard",
                                                  "org.ukui.ukcc.session.KeyBoard",
                                                  "changed",
                                                  this,
                                                  SLOT(dataChanged(QString)));

        }
    }
    return keyBoardWidget;
}

const QString KeyboardMain::name() const
{
    return QStringLiteral("Keyboard");
}

bool KeyboardMain::isShowOnHomePage() const
{
    return true;
}

QIcon KeyboardMain::icon() const
{
    return QIcon::fromTheme("input-keyboard-symbolic");
}

bool KeyboardMain::isEnable() const
{
    return !Common::isWayland();
}

void KeyboardMain::initConnection()
{
    // 界面设置发生变化
    connect(keyBoardWidget->keyRepeatFrame(), &SwitchWidget::stateChanged, this, &KeyboardMain::keyRepeatSlot);

    connect(keyBoardWidget->delayFrame(), &SliderWidget::valueChanged, this, &KeyboardMain::keyDelaySlot);

    connect(keyBoardWidget->speedFrame(), &SliderWidget::valueChanged, this, &KeyboardMain::keySpeedSlot);

    connect(keyBoardWidget->keyTipsFrame(), &SwitchWidget::stateChanged, this, &KeyboardMain::keyTipsSlot);

    connect(keyBoardWidget->inputMethodPushButton(), &QPushButton::clicked, this, &KeyboardMain::inputMethodSetSlot);

}

void KeyboardMain::initKeyboardStatus()
{
    initKeyRepeat();

    initDelay();

    initSpeed();

    initKeyTips();
}

void KeyboardMain::initKeyRepeat()
{
    //设置按键重复状态
    keyBoardWidget->keyRepeatFrame()->blockSignals(true);
    bool checked = keyBoardDbus->property("repeat").toBool();
    keyBoardWidget->keyRepeatFrame()->setChecked(checked);
    keyBoardWidget->keyRepeatFrame()->blockSignals(false);
    setKeyboardVisible(keyBoardWidget->keyRepeatFrame()->isChecked());
}

void KeyboardMain::initDelay()
{
    //设置按键重复的延时
    keyBoardWidget->delayFrame()->slider()->blockSignals(true);
    int value = keyBoardDbus->property("delay").toInt();
    keyBoardWidget->delayFrame()->setValue(value);
    keyBoardWidget->delayFrame()->slider()->blockSignals(false);
}

void KeyboardMain::initSpeed()
{
    //设置按键重复的速度
    keyBoardWidget->speedFrame()->slider()->blockSignals(true);
    int value = keyBoardDbus->property("rate").toInt();
    keyBoardWidget->speedFrame()->setValue(value);
    keyBoardWidget->speedFrame()->slider()->blockSignals(false);

}

void KeyboardMain::initKeyTips()
{
    //设置按键提示状态
    keyBoardWidget->keyTipsFrame()->blockSignals(true);
    bool checked = keyBoardDbus->property("showLockTip").toBool();
    keyBoardWidget->keyTipsFrame()->setChecked(checked);
    keyBoardWidget->keyTipsFrame()->blockSignals(false);
}

void KeyboardMain::setKeyboardVisible(bool checked)
{
    keyBoardWidget->delayFrame()->setVisible(checked);
    keyBoardWidget->speedFrame()->setVisible(checked);
    keyBoardWidget->inputTestFrame()->setVisible(checked);
}

void KeyboardMain::dataChanged(QString key)
{
    if (key == QString("repeat")) {
        initKeyRepeat();
    } else if (key == QString("delay")) {
        initDelay();
    } else if (key == QString("rate")) {
        initSpeed();
    }else if (key == QString("show-lock-tip")) {
        initKeyTips();
    }
}

/* Slot */
void KeyboardMain::keyRepeatSlot(bool checked)
{
    setKeyboardVisible(checked);
    keyBoardDbus->call("setKeyRepeat", checked);
    Common::buriedSettings(name(), keyBoardWidget->keyRepeatFrame()->objectName(), QString("setting"), checked ? "true":"false");
}

void KeyboardMain::keyDelaySlot(int value)
{
    keyBoardDbus->call("setDelay", value);
}

void KeyboardMain::keySpeedSlot(int value)
{
    keyBoardDbus->call("setSpeed", value);
}

void KeyboardMain::keyTipsSlot(bool checked)
{
    keyBoardDbus->call("setKeyTips", checked);
    Common::buriedSettings(name(), keyBoardWidget->keyTipsFrame()->objectName(), QString("setting"), checked ? "true":"false");
}

void KeyboardMain::inputMethodSetSlot()
{
    Common::buriedSettings(name(), keyBoardWidget->inputMethodPushButton()->objectName(), QString("setting"));

    QProcess process;
    process.startDetached("fcitx5-config-qt");
}

bool KeyboardMain::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::FocusOut) {
        if (watched == keyBoardWidget->delayFrame()->slider()) {
            int value = keyBoardWidget->delayFrame()->value();
            Common::buriedSettings(name(), keyBoardWidget->delayFrame()->objectName(), QString("setting"), QString::number(value));
        } else if (watched == keyBoardWidget->speedFrame()->slider()) {
            int value = keyBoardWidget->speedFrame()->value();
            Common::buriedSettings(name(), keyBoardWidget->speedFrame()->objectName(), QString("setting"), QString::number(value));
        }
    }
    return QObject::eventFilter(watched, event);
}

